<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:insert="~{include :: header('角色分配用户')}" />
	<th:block th:insert="~{include :: bootstrap-table-css}" />
</head>
<body class="hold-transition">
<div class="container-fluid mb-2">
	<div class="toolbar" id="toolbar" role="group">
		<a class="btn btn-success btn-sm" onclick="selectUser()" shiro:hasPermission="role:add">
			<i class="fa fa-plus"></i> 添加用户
		</a>
		<a class="btn btn-danger btn-sm multiple disabled" onclick="cancelAuthUserAll()" shiro:hasPermission="role:remove">
			<i class="fa fa-remove"></i> 批量取消
		</a>
	</div>

	<table id="bootstrap-table" class="table-striped"></table>
</div>
<th:block th:insert="~{include :: footer}" />
<th:block th:insert="~{include :: bootstrap-table-js}" />
<script th:inline="javascript">
	var removeFlag = [[${@permission.hasPermi('role:remove')}]];
	var datas = [[${@dict.getType('sys_normal_disable')}]];
	var prefix = "/role/auth";
	var roleId = [[${role.roleId}]];

	$(function() {
		var options = {
			showSearch: false,
			showRefresh: false,
			showToggle: false,
			showColumns: false,
			url: prefix + "/list.json",
			queryParams: queryParams,
			sortName: "createTime",
			sortOrder: "desc",
			modalName: "用户",
			columns: [{
				checkbox: true
			},
				{
					field: 'managerId',
					title: '用户ID',
					visible: false
				},
				{
					field: 'loginName',
					title: '登录名称',
					sortable: true
				},
				{
					field: 'managerName',
					title: '用户名称'
				},
				{
					field: 'mobile',
					title: '手机'
				},
				{
					field: 'isDisabled',
					title: '用户状态',
					align: 'center',
					formatter: function (value, row, index) {
						return $.table.selectDictLabel(datas, value);
					}
				},
				{
					field: 'createTime',
					title: '创建时间'
				},
				{
					title: '操作',
					align: 'center',
					formatter: function(value, row, index) {
						var actions = [];
						actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="cancelAuthUser(\'' + row.managerId + '\')"><i class="fa fa-remove"></i>取消授权</a> ');
						return actions.join('');
					}
				}]
		};
		$.table.init(options);
	});

	function queryParams(params) {
		var search = $.table.queryParams(params);
		search.roleId = roleId;
		return search;
	}

	/* 分配用户-选择用户 */
	function selectUser() {
		var callback = function(index, layero) {
			var iframeWin = layero.find('iframe')[0];
			iframeWin.contentWindow.submitHandler(index, layero);
		}
		$.modal.open('选择用户', prefix + '/selectUser/' + roleId, '70%', '90%',callback);
	}

	/* 分配用户-批量取消授权 */
	function cancelAuthUserAll(userId) {
		var rows = $.table.selectFirstColumns();
		if (rows.length == 0) {
			$.modal.alertWarning("请至少选择一条记录");
			return;
		}
		$.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
			var data = { "roleId": roleId, "userIds": rows.join() };
			$.operate.submit(prefix + "/removeAll.json", "post", "json", data);
		});
	}

	/* 分配用户-取消授权 */
	function cancelAuthUser(managerId) {
		$.modal.confirm("确认要取消该用户角色吗？", function() {
			$.operate.post(prefix + "/remove.json", { "roleId": roleId, "managerId": managerId });
		})
	}
</script>
</body>
</html>